<?php
/**
 * Created by PhpStorm.
 * User: jiangxiaobo
 * Date: 2019/11/11 0011
 * Time: 20:11
 */
namespace routes;

use bootstrap\lib\Request;

abstract class Base
{
    abstract protected function getRoutes();
    abstract protected function getDefaultController();
    abstract protected function getSplitString();

    public function trigger()
    {
        $routes = $this->getRoutes();
        if(IS_CLI) {

        } else if(Request::getInstance()->isAjax()) {

        } else {
            // /api.php/dsfsd/dasdas?adsda=sasa
            $flag = false;
            foreach($routes as $pattern => $route)
            {
                if(preg_match("/{$pattern}/", Request::getInstance()->server('REQUEST_URI','')))
                {
                    $t = explode("->", $route);
                    if(empty($t[1]))
                    {
                        $t[1] = 'index';
                    }
                    // 反射实例化控制器类
                    $reflectionClass = new \ReflectionClass($t[0]);
                    $cInstance = $reflectionClass->newInstance();
                    if(!method_exists($cInstance,$t[1])){
                        throw new \Exception("{$t[0]}控制器{$t[1]}方法不存在");
                    }
//                    call_user_func_array([$cInstance,$a],[param1,param2]);
//                    call_user_func([$cInstance,$a],param1,param2);
                    call_user_func([$cInstance,$t[1]]);

                    $flag = true;
                    break;
                }
            }

            if(!$flag)
            {
                header("HTTP/1.1 404 Not Found");
                header("Status: 404 Not Found");
            }
        }
    }
}